<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/message.jsp"%>
<%
    Integer orderId = input.getInt("order_id", -1);
    Integer doctorUid = input.getInt("doctor_uid", -1);
    String doctorName = input.getDesc("doctor_name", "");
    String date = input.getDesc("date", "");
    Integer half = input.getInt("half", -1);
    List<Order> orders = QBuilder.db("basedb").select().from("Order").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", orderId)).query().getResultObj();
    if (isEmpty(orders)) {
        msg = new Message(30041, "预约信息有误");
        Tools.encryptPrint(out, msg);
        return;
    }
    if (doctorUid == -1) {
        msg = new Message(30041, "请选择医生");
        Tools.encryptPrint(out, msg);
        return;
    }
    if (isEmpty(date)) {
        msg = new Message(30041, "请选择日期");
        Tools.encryptPrint(out, msg);
        return;
    }
    Order order = orders.get(0);
    order.setState(STATE_HANDLED);
    order.setExecTime(DateUtil.parseDate(date).getTime());
    order.setDoctor(doctorUid);
    order.setDoctorName(doctorName);
    if (half != -1) {
        order.setHalf(half);
    }
    DBTools.db("basedb").modItem(order, orderId);
    Information info = new Information();
    info.setHospital(hospital);
    info.setOid(orderId+"");
    info.setTitle("服务安排通知");
    info.setSumary("服务安排通知");
    String _half = Tools.getDesc(order.getHalf(), "");
    _half = "1".equals(_half) ? "上午" : "2".equals(_half) ? "下午" : "";
    date = DateUtil.formatDate(new Date(order.getExecTime()));
    String week = getWeekday(date);
    String content = Tools.getDesc(order.getDoctorName(), "") + "医生,已为您安排了" + date + " " + week + _half + ""+ Tools.getDesc(order.getUserName(), "") + "的" + Tools.getDesc(order.getTitle(), "") + "服务";
    info.setCategory(3);
    info.setContent(content);
    createMsg(info, doctorUid);      // 医生消息
    content = Tools.getDesc(order.getUserName(), "") + ",已为您安排了" + date + " " + week + _half + ""+ Tools.getDesc(order.getDoctorName(), "") + "的" + Tools.getDesc(order.getTitle(), "") + "服务";
    info.setContent(content);
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", order.getUid())).query().getResultObj();
    if (!isEmpty(userProfiles)) {
        createMsg(info, userProfiles.get(0).getUid());      // 病人消息
    }
    Tools.encryptPrint(out, msg);
%>
